﻿@model OrderModel.AddOrderProductModel
@{
    var gridPageSize = EngineContext.Current.Resolve<Nop.Core.Domain.Common.AdminAreaSettings>().GridPageSize;

    //page title
    ViewBag.Title = string.Format(T("Admin.Orders.Products.AddNew.Title1").Text, Model.OrderId);
}
@using Telerik.Web.Mvc.UI;
<div class="section-header">
    <div class="title">
        <img src="@Url.Content("~/Administration/Content/images/ico-sales.png")" alt="" />
        @string.Format(T("Admin.Orders.Products.AddNew.Title1").Text, Model.OrderId) @Html.ActionLink("(" + T("Admin.Orders.Products.AddNew.BackToOrder") + ")", "Edit", new { id = Model.OrderId })
    </div>
    <div class="options">
    </div>
</div>
<table width="100%">
    <tr>
        <td class="adminTitle">
            @Html.NopLabelFor(model => model.SearchProductName):
        </td>
        <td class="adminData">
            @Html.EditorFor(model => Model.SearchProductName)
        </td>
    </tr>
    <tr>
        <td class="adminTitle">
            @Html.NopLabelFor(model => model.SearchCategoryId):
        </td>
        <td class="adminData">
            @Html.DropDownList("SearchCategoryId", Model.AvailableCategories)
        </td>
    </tr>
    <tr>
        <td class="adminTitle">
            @Html.NopLabelFor(model => model.SearchManufacturerId):
        </td>
        <td class="adminData">
            @Html.DropDownList("SearchManufacturerId", Model.AvailableManufacturers)
        </td>
    </tr>
    <tr>
        <td class="adminTitle">
            @Html.NopLabelFor(model => model.SearchProductTypeId):
        </td>
        <td class="adminData">
            @Html.DropDownList("SearchProductTypeId", Model.AvailableProductTypes)
        </td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="button" id="search-products" class="t-button" value="@T("Admin.Common.Search")" />
        </td>
    </tr>
</table>
<table class="adminContent">
    <tr>
        <td>
            <strong>@T("Admin.Orders.Products.AddNew.Note1")</strong>
        </td>
    </tr>
    <tr>
        <td>
            @(Html.Telerik().Grid<OrderModel.AddOrderProductModel.ProductModel>()
                    .Name("products-grid")
                    .Columns(columns =>
                    {
                        columns.Bound(p => p.Id).Width(70);
                        columns.Bound(p => p.Name).Width(400);
                        columns.Bound(p => p.Sku).Width(100);
                    })
                    .Selectable()
                    .Pageable(settings => settings.PageSize(gridPageSize).Position(GridPagerPosition.Both))
                            .DataBinding(dataBinding => dataBinding.Ajax().Select("AddProductToOrder", "Order"))
                    .ClientEvents(events => events.OnDataBinding("onDataBinding").OnRowSelected("onRowSelected"))
                    .EnableCustomBinding(true))
        </td>
    </tr>
</table>
<div id="productdetails">
</div>
@Html.HiddenFor(model => Model.OrderId)
<script type="text/javascript">
    $(document).ready(function () {
        $('#search-products').click(function () {
            var grid = $('#products-grid').data('tGrid');
            grid.currentPage = 1; //new search. Set page size to 1
            grid.ajaxRequest();
            return false;
        });
    });

    $("#@Html.FieldIdFor(model => model.SearchProductName)").keydown(function (event) {
        if (event.keyCode == 13) {
            $("#search-products").click();
            return false;
        }
    });

    function onDataBinding(e) {
        var searchModel = {
            SearchProductName: $('#@Html.FieldIdFor(model => model.SearchProductName)').val(),
            SearchCategoryId: $('#SearchCategoryId').val(),
            SearchManufacturerId: $('#SearchManufacturerId').val(),
            SearchProductTypeId: $('#SearchProductTypeId').val()
        };
        e.data = searchModel;
    }

    function onRowSelected(e) {
        productID = e.row.cells[0].innerHTML;
        //load "product details page" block
        var productDetailsActionUrl = '@Html.Raw(Url.Action("AddProductToOrderDetails", "Order", new { orderId = Model.OrderId, productId = "productidplaceholder" }))';
        productDetailsActionUrl = productDetailsActionUrl.replace("productidplaceholder", productID);
        setLocation(productDetailsActionUrl);
    }
</script>
